//
//  BIDBrowserImageViewController.m
//  zhongrongbao
//
//  Created by mal on 15/7/3.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDBrowserImageViewController.h"
#import "BIDPreviewImgView.h"
#import "BIDImgDownloader.h"
#import "BIDCollectionViewCell.h"
#import "BIDScrollViewWithImage.h"
#import "BIDNode.h"

@interface BIDBrowserImageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    /**
     *导航栏的titleView
     */
    UIView *_titleViewForNav;
    //
    UICollectionView *_collectionView;
    
    BOOL _bRegister;
}
@end

@implementation BIDBrowserImageViewController
@synthesize imgArr;
@synthesize index;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor blackColor];
    [self createOtherView];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createOtherView
{
    _titleViewForNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 22)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_titleViewForNav.frame), CGRectGetHeight(_titleViewForNav.frame))];
    label.tag = 1;
    [label setFont:[UIFont systemFontOfSize:22.0f]];
    [label setTextColor:[UIColor whiteColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setText:[[NSString alloc] initWithFormat:@"%ld of %ld", index+1, imgArr.count]];
    [_titleViewForNav addSubview:label];
    self.navigationItem.titleView = _titleViewForNav;
    //create collectionView
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat ownWidth = screenSize.width;
    CGFloat ownHeight = screenSize.height;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setMinimumInteritemSpacing:0.0f];
    [flowLayout setMinimumLineSpacing:0.f];
    [flowLayout setItemSize:CGSizeMake(ownWidth, ownHeight-64-44)];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ownWidth, ownHeight-64-44) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    [self.view addSubview:_collectionView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(![scrollView isKindOfClass:[UICollectionView class]]) return;
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    UILabel *label = (UILabel*)[_titleViewForNav viewWithTag:1];
    [label setText:[[NSString alloc] initWithFormat:@"%ld of %ld", page+1, imgArr.count]];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return imgArr.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    static NSString *identifier = @"identifier";
    if(!_bRegister)
    {
        _bRegister = YES;
        [collectionView registerClass:[BIDCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    }
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    BIDPreviewImgView *preview = (BIDPreviewImgView*)[cell.contentView viewWithTag:100];
    if(!preview)
    {
        preview = [[BIDPreviewImgView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(collectionView.frame), CGRectGetHeight(collectionView.frame))];
        preview.tag = 100;
        [cell.contentView addSubview:preview];
    }
    [preview.scrollViewWithImg setZoomScale:1.0f];
    preview.scrollViewWithImg.imgView.image = imgArr[row];
    return cell;
}

@end
